﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AgileBoard.Domain;

namespace AgileBoard.Demo
{
    public class FakeProjectManager
    {
        private readonly List<Project> projectList;

        public FakeProjectManager(List<Project> projects)
        {
            projectList = projects;
        }

        /// <summary>
        /// Create a project (back to contain the default value)
        /// </summary>
        /// <returns></returns>
        public Project NewProject()
        {
            var project = new Project();

            project.CreateTime = DateTime.Now;

            return project;
        }

        /// <summary>
        /// Save project
        /// </summary>
        /// <param name="project">Project entity</param>
        /// <returns></returns>
        public Project Save(Project project)
        {
            Validate(project);
            if (Get(project.ID) != null)
                throw new Exception("Existing project");
            project.ID = Guid.NewGuid();
            projectList.Add(project);

            return Get(project.ID);
        }

        /// <summary>
        /// Access to projects
        /// </summary>
        /// <param name="projectId">Project ID</param>
        /// <returns></returns>
        public Project Get(Guid projectId)
        {
            return projectList.SingleOrDefault(u => u.ID == projectId);
        }

        /// <summary>
        /// Access to projects
        /// </summary>
        /// <param name="projectId">Project ID</param>
        /// <returns></returns>
        public Project GetByName(string projectName)
        {
            return projectList.SingleOrDefault(u => u.Name == projectName);
        }

        /// <summary>
        /// Delete Project
        /// </summary>
        /// <param name="projectId">Project ID</param>
        /// <returns></returns>
        public void Delete(Guid projectId)
        {
            Project project = Get(projectId);
            projectList.Remove(project);
        }

        /// <summary>
        /// Update Project
        /// </summary>
        /// <param name="project">Project Entity</param>
        /// <returns></returns>
        public Project Update(Project project)
        {
            return Get(project.ID);
        }

        /// <summary>
        /// Access to projects
        /// </summary>
        /// <returns></returns>
        public IList GetAll()
        {
            return projectList;
        }

        private void Validate(Project project)
        {
            //TODO throw exception on error.
        }
    }
}